package gruppe7.servlets;


import gruppe7.data.UserController;
import gruppe7.tlrs.*;
import gruppe7.util.Html;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LookUpPrice extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Html html = new Html();

    public LookUpPrice() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		RequestDispatcher rd = request.getRequestDispatcher("main_links.jsp"); 
		UserController uc = UserController.getInstance();
		
		out.println(html.setHeader("Lookup Ticket Price"));
		out.println("<body>");
	    rd.include (request, response);
	    
	    out.println("<div id='login'>");
	   
	    if(uc.isValidSession(request))
	    	out.println("<p class='blue'>[ You are logged in as " + uc.getSessionUser(request).getUsername() + " - <a href='Logout'>logout?</a> ]</p>");
	    
	    else
	    	out.println("<p class='blue'>[ You are not logged in ]</p>");
	    
	    out.println("</div>");
	    
	    out.println("</div></div><div id ='content'>");
	    out.println("<div id='content-left'>");
	    out.println("<h3><img src='images/tickets.png' /> Ticket Lookups</h3><br/>");
	    
	    if(uc.isValidSession(request))
	    {
	    	if(request.getAttribute("same")!=null)
	    	{
	    		out.println("<p class='red'>Please pick another destination than your departure");
	    	}
	    	
	    	dropDownLists(out);
	    }
	    else
	    {
	    	out.println("<p><a href='login.jsp'>You must be logged in to do this - click here</a></p>");
	    }
	    out.println("</div></div>");
		out.println(html.setFooter());
		
	}

	private void dropDownLists(PrintWriter out) {
		
		TLRSControl tickC = TLRSControl.getInstance();
		tickC.setStations();
		out.println("" +
				"<p><table class='default'>" +
				"<form action='TicketPrice' method='post'>" +
				"<tr><td>Departure</td>" +
				"<td><select name='departure'>");
		
		for(Station s: tickC.getStations().values())
		{
		
			out.println("<option value='"+s.getName()+"'>"+s.getName()+"</option>");
		}
		
		out.println("</select></td></tr></br>" +
				"<tr><td>Destination</td>" +
				"<td><select name='destination'>");
		
		for(Station s: tickC.getStations().values())
		{
			out.println("<option value='"+s.getName()+"'>"+s.getName()+"</option>");
		}
		
		out.println("</select></td></tr></br>" +
				"<tr><td></td><td align='right'><input class='submit' type='submit' value='Inform me!'/></td></tr>" +
				"</form></table></p>");
	}

}
